<?php
declare (strict_types = 1);

namespace app\admin\controller;
use think\app;
use app\AppApi;
use think\Console;
use think\facade\View;
use think\facade\Db;
use think\facade\Cookie;
use app\admin\model\AdminConfig;
use think\facade\Request;


class Login
{
    public function index()
    {
        $aAdminConfig = new AdminConfig();
		$this->config = $aAdminConfig->getAll();
		View::assign([
			'config' => $this->config
		]);
       return view::fetch();
    }

    public function login()
    {
        if (request::isPost())
        {
            $account =  trim(input('post.account'));
            $password =  trim(input('post.password'));
            $code = trim(input('post.code'));
            $remember = input('remember');
            if (empty($account))
            {
                $this->returnCode('90000001');
            }

            if (empty($password))
            {
                $this->returnCode(1,'密码不能为空');
            }
            if (empty($code))
            {
                $this->returnCode(1,'验证码不能为空');
            }
            if (!captcha_check($code)){
                $this->returnCode(1,'验证码错误');
            }

          $user =  Db::table('admin_user')->where('account',$account)->find();

            if (empty($user))
            {
                $this->returnCode('90000029');
            }
            if ($user['status'] != 1) {
                $this->returnCode('90000030');
            }
              if ($user['password'] != md5($password))
              {
                  $this->returnCode('90000031');
              }
              if (!empty($remember))
              {
                  Cookie::set('admin_id',$user['uid'],60*60*24*7);
                  Cookie::set('admin_name',$user['name'],60*60*24*7);
              }else
              {
                  Cookie::set('admin_id',$user['uid']);
                  Cookie::set('admin_name',$user['name']);

              }

            Db::table('admin_user')->where('uid',$user['uid'])->update(
                ['times_login' => $user['times_login']+1,'time_last' => time()]
            );
            $this->returnCode(0,[],'登陆成功');
        }
        
    }

    protected function returnCode($code,$data=[],$msg=''){
        header('Content-type:application/json');
        if($code == 0){
            $arr = array(
                'code'=>$code,
                'msg'=>$msg,
                'data' => $data
            );
        }else if($code == 1){
            $arr = array(
                'code'	=>	1,
                'msg'	=>	$data
            );
        }else{
            $appapi = new AppApi();
            $arr = array(
                'code'=>$code,
                'msg'=>$appapi::errorTip($code)
            );
        }
        echo json_encode($arr);
        if($code != 0){
            exit;
        }
    }

    public function logout(){
        Cookie::delete('admin_id');
        Cookie::delete('admin_name');
        $this->returnCode(0,[],'退出成功');
    }
}
